<?php

/* function that may be used by the fop */

class fop_Object_Structure {

	protected $_displayName = '';
	protected $_name = '';
	protected $_params = array();
	protected $_childrenStructures = array();
 
	static protected $_allowedRenderMode = array('FOPTable','RegularExpression');

  
	public function __construct($type,$data = array()) {
		if(empty($data)) {
			if(empty($type)) {
			
			}
		}
		//print_r($data);
		//exit;
		$this->_name = $data['name'];
		$this->_displayName = @$data['displayName'];
		
		//we fill the variables
		if(!empty($data['values'])) {
			foreach($data['values'] as $num => $param) {
				$this->_params[$param['name']] = $param['value'];
			}
		}
		
		if(!empty($data['child'])) {
			foreach($data['child'] as $num => $structureData) {
				$this->_childrenStructures[] = new fop_Object_Structure('',$structureData);
			}			
		}
		
		
	} //end constructor
	
	
	public function render($mode,$options = array())	{
		if(!in_array($mode,self::$_allowedRenderMode)) {
			require_once('fop/objects/Exception.php');
			throw new fop_Object_Exception('Mode is not an authorized render mode',35);		
		}

    Zend_Registry::get('dt_log')->log('rendering '.$this->_name.' - '.$this->_displayName.' - options:'.serialize($options), Zend_Log::DEBUG);
		
		if($mode == 'FOPTable') {
			

			$renderString = '';
			
			
			
			if($this->_name == 'group') {
				if(isset($options['bypassOptional'])) $bypassOptional = $options['bypassOptional'];
				else $bypassOptional = false;
				if($this->_params['condition'] == 'optional' && !$bypassOptional) {
					$renderString .= '?'; 
				}
			
				$structuresRender = array();
					
				foreach($this->_childrenStructures as $num => $structureObject) {
					$childBypassOPtional = !$bypassOptional;
					$structuresRender[] = $structureObject->render($mode,array('bypassOptional' => $childBypassOPtional));
				}
				
				
				
				if($this->_params['relationship'] == 'exclusive or') {
					$renderString .= implode('!',$structuresRender);
				} else {
					$renderString .= '<';
					$renderString .= implode(',',$structuresRender);
					$renderString .= '>';
				}
				

			} //end structure group
			
			
			
			
			elseif($this->_name == 'string') {
				if($this->_params['condition'] == 'optional') {
					$renderString .= '?'; 
				}

				if(!empty($this->_params['crypticBefore'])) {
					$renderString .= $this->_params['crypticBefore']; 
				}
				
				$renderString .= '@B'.$this->_params['positionId']; 				
				
				if($this->_params['format'] == 'numeric') {
					$renderString .= '@NUM';
				}
				if($this->_params['format'] == 'alpha') {
					$renderString .= '@ALP';
				}
				if($this->_params['format'] == 'alphanumeric') {
					$renderString .= '@ALN';
				}				
				if(empty($this->_params['max']) || $this->_params['min'] == $this->_params['max']) $renderString .= $this->_params['min'];
				else $renderString .= $this->_params['min'].'-'.$this->_params['max'];
			} //end structure string

			
			
			
			elseif($this->_name == 'date') {
				if($this->_params['condition'] == 'optional') {
					$renderString .= '?'; 
				}
				if(!empty($this->_params['crypticBefore'])) {
					$renderString .= $this->_params['crypticBefore']; 
				}				
				$renderString .= '@CCDTE1';
			} //end structure date	




			
			elseif($this->_name == 'creditCardNum') {
				if($this->_params['condition'] == 'optional') {
					$renderString .= '?'; 
				}
				if(!empty($this->_params['crypticBefore'])) {
					$renderString .= $this->_params['crypticBefore']; 
				}				
				if(!empty($this->_params['bypassAuthor']) && $this->_params['bypassAuthor']) {
					$renderString .= '@3';
				}
				
				$renderString .= '@CCNUM';
				
				if(empty($this->_params['max']) || $this->_params['min'] == $this->_params['max']) $renderString .= $this->_params['min'];
				else $renderString .= $this->_params['min'].'-'.$this->_params['max'];

			} //end structure credit card num
			
			
			
			//?<*CV@35,I@81>!<*CV@35,M@82>!<*CV@35,A@85>!<*CV@35,X@85>!<*CV35,N@85>!<*CV@35,U@85>!<*CV@35,S@85>!<*CV@35>,?<@36@NUM3>
			elseif($this->_name == 'CVV') {

				//first the CV verification group is always optional
				$renderString .= '?'; 
				
				$cryptic = (isset($this->_params['crypticBefore']) && !empty($this->_params['crypticBefore']))?$this->_params['crypticBefore']:'*';
			
				if(!empty($this->_params['vendor']) && $this->_params['vendor'] == 'visa') {
					$renderString .= '<'.$cryptic.'CV@35,I@81>!<'.$cryptic.'CV@35,M@82>!';
				}
				
				$renderString .= '<'.$cryptic.'CV@35,A@85>!<'.$cryptic.'CV@35,X@85>!<'.$cryptic.'CV35,N@85>!<'.$cryptic.'CV@35,U@85>!<'.$cryptic.'CV@35,S@85>!<'.$cryptic.'CV@35>,';
				
				if($this->_params['condition'] == 'optional') $renderString .= '?';
				$renderString .= '<@36@NUM'.$this->_params['nbDigits'].'>';
				

			} //end structure CVV			
			



			elseif($this->_name == 'manualApprovalCode') {
				if($this->_params['condition'] == 'optional') {
					$renderString .= '?'; 
				}
				if(!empty($this->_params['crypticBefore'])) {
					$renderString .= $this->_params['crypticBefore']; 
				}				

				
				$renderString .= '@2@CCALN';
				
				if(empty($this->_params['max']) || $this->_params['min'] == $this->_params['max']) $renderString .= $this->_params['min'];
				else $renderString .= $this->_params['min'].'-'.$this->_params['max'];

			} //end structure manual approval code			



			elseif($this->_name == 'automaticApprovalCode') {
				if($this->_params['condition'] == 'optional') {
					$renderString .= '?'; 
				}
				if(!empty($this->_params['crypticBefore'])) {
					$renderString .= $this->_params['crypticBefore']; 
				}				

				
				$renderString .= '@3@CCALN';
				
				if(empty($this->_params['max']) || $this->_params['min'] == $this->_params['max']) $renderString .= $this->_params['min'];
				else $renderString .= $this->_params['min'].'-'.$this->_params['max'];

			} //end structure manual approval code

	


			elseif($this->_name == 'amount') {
				if($this->_params['condition'] == 'optional') {
					$renderString .= '?'; 
				}
				if(!empty($this->_params['crypticBefore'])) {
					$renderString .= $this->_params['crypticBefore']; 
				}				

				
				$renderString .= '@AMTA';

			} //end structure manual approval code

	
			
			
			elseif($this->_name == 'dataset') {
				if($this->_params['condition'] == 'optional') {
					$renderString .= '?'; 
				}
				if(!empty($this->_params['crypticBefore'])) {
					$renderString .= $this->_params['crypticBefore']; 
				}				
				
				$renderString .= '@B'.$this->_params['positionId']; 
				
				
				$list = array();
				for($i=1;$i<=5;$i++)
				if(!empty($this->_params['value'.$i])) $list[] = $this->_params['value'.$i];
				
				$renderString .= implode('!',$list);
				
			} //end structure dataset
			
			
			
			
			
			
			Zend_Registry::get('dt_log')->log('rendered '.$this->_displayName.':'.$renderString, Zend_Log::DEBUG);			
			return $renderString;
			
		} //end mode = FOPTable













		if('RegularExpression' == $mode) {
			$renderString = '';
			
			$pattern = array('/\//','/\*/','/\./');
			$replacement = array('\/','\*','\.');
			
			
			
			if($this->_name == 'group') {
				if(isset($options['bypassOptional'])) $bypassOptional = $options['bypassOptional'];
				else $bypassOptional = false;

				$renderString .= '('; 
		
			
				$structuresRender = array();
					
				foreach($this->_childrenStructures as $num => $structureObject) {
					$childBypassOPtional = !$bypassOptional;
					$structuresRender[] = $structureObject->render($mode,array('bypassOptional' => $childBypassOPtional));
				}
				
				if($this->_params['relationship'] == 'exclusive or') {
					$renderString .= implode('|',$structuresRender);
				} else {
					$renderString .= implode('',$structuresRender);
				}

				if($this->_params['condition'] == 'optional' && !$bypassOptional) {
					$renderString .= ')?'; 
				}	else {
					$renderString .= ')'; 
				}				

			} //end structure group
			
			
			
			
			elseif($this->_name == 'string') {
				$renderString .= '(';

				if(!empty($this->_params['crypticBefore'])) {
					$renderString .= preg_replace($pattern,$replacement,$this->_params['crypticBefore']); 
				}			

				$renderString .= '(';
				
				if($this->_params['format'] == 'numeric') {
					$renderString .= '?P<match_'.$this->_params['positionId'].'>[0-9]';
				}
				if($this->_params['format'] == 'alpha') {
					$renderString .= '?P<match_'.$this->_params['positionId'].'>[a-zA-Z]';
				}
				if($this->_params['format'] == 'alphanumeric') {
					$renderString .= '?P<match_'.$this->_params['positionId'].'>[a-zA-Z0-9]';
				}				
				if(empty($this->_params['max']) || $this->_params['min'] == $this->_params['max']) $renderString .= '{'.$this->_params['min'].'}';
				else $renderString .= '{'.$this->_params['min'].','.$this->_params['max'].'}';
			
				$renderString .= ')';				
			
				if($this->_params['condition'] == 'optional') {
					$renderString .= ')?'; 
				} else $renderString .= ')';
			} //end structure string

			
			
			
			elseif($this->_name == 'date') {
				$renderString .= '('; 
				
				if(!empty($this->_params['crypticBefore'])) {
					$renderString .= preg_replace($pattern,$replacement,$this->_params['crypticBefore']);  
				}				
				$renderString .= '(?P<matchdate>[0-9]{4})';
				if($this->_params['condition'] == 'optional') {
					$renderString .= ')?'; 
				} else $renderString .= ')';
			} //end structure date	
			
			elseif($this->_name == 'creditCardNum') {
				$renderString .= '('; 
				$renderString .= '?P<matchcreditCardNum>[0-9]';
				if(empty($this->_params['max']) || $this->_params['min'] == $this->_params['max']) $renderString .= '{'.$this->_params['min'].'}';
				else $renderString .= '{'.$this->_params['min'].','.$this->_params['max'].'}';
				
				if($this->_params['condition'] == 'optional') {
					$renderString .= ')?'; 
				} else $renderString .= ')';

			} //end structure credit card num
			
			
			
			//?<*CV@35,I@81>!<*CV@35,M@82>!<*CV@35,A@85>!<*CV@35,X@85>!<*CV35,N@85>!<*CV@35,U@85>!<*CV@35,S@85>!<*CV@35>,?<@36@NUM3>
			elseif($this->_name == 'CVV') {
				$renderString .= '('; 
				$cryptic = (isset($this->_params['crypticBefore']) && !empty($this->_params['crypticBefore']))?$this->_params['crypticBefore']:'*';
				$renderString .= preg_replace($pattern,$replacement,$cryptic);
				$renderString .= '(CV(?P<matchCVV>[0-9]{'.$this->_params['nbDigits'].'}))';
				if($this->_params['condition'] == 'optional') {
					$renderString .= ')?'; 
				} else $renderString .= ')';
			} //end structure CVV			
			

			elseif($this->_name == 'manualApprovalCode') {
				$renderString .= '('; 
				
				if(!empty($this->_params['crypticBefore'])) {
					$renderString .= preg_replace($pattern,$replacement,$this->_params['crypticBefore']);  
				}	
				
				$renderString .= '(?P<matchmanualApprovalCode>[0-9]';
				if(empty($this->_params['max']) || $this->_params['min'] == $this->_params['max']) $renderString .= '{'.$this->_params['min'].'}';
				else $renderString .= '{'.$this->_params['min'].','.$this->_params['max'].'})';
				
				if($this->_params['condition'] == 'optional') {
					$renderString .= ')?'; 
				} else $renderString .= ')';

			} //end structure manual approval code			


			elseif($this->_name == 'automaticApprovalCode') {
				$renderString .= '('; 
				
				if(!empty($this->_params['crypticBefore'])) {
					$renderString .= preg_replace($pattern,$replacement,$this->_params['crypticBefore']);  
				}	
				
				$renderString .= '(?P<matchautomaticApprovalCode>[0-9]';
				if(empty($this->_params['max']) || $this->_params['min'] == $this->_params['max']) $renderString .= '{'.$this->_params['min'].'}';
				else $renderString .= '{'.$this->_params['min'].','.$this->_params['max'].'})';
				
				if($this->_params['condition'] == 'optional') {
					$renderString .= ')?'; 
				} else $renderString .= ')';

			} //end structure manual approval code


	


			elseif($this->_name == 'amount') {
				$renderString .= '('; 
				if(!empty($this->_params['crypticBefore'])) {
					$renderString .= preg_replace($pattern,$replacement,$this->_params['crypticBefore']);  
				}	
				$renderString .= '(?P<matchamount>(?P<matchcurrency>[a-zA-Z]{3})(?P<matchamountValue>[0-9]{1,12}(\.[0-9]{1,3})?))';
				
				if($this->_params['condition'] == 'optional') {
					$renderString .= ')?'; 
				} else $renderString .= ')';

			} //end structure manual approval code

	
			
			
			elseif($this->_name == 'dataset') {
				$renderString .= '('; 
				if(!empty($this->_params['crypticBefore'])) {
					$renderString .= preg_replace($pattern,$replacement,$this->_params['crypticBefore']);  
				}	

				$list = array();
				for($i=1;$i<=5;$i++)
				if(!empty($this->_params['value'.$i])) $list[] = $this->_params['value'.$i];
				
				$renderString .= '(?P<match_'.$this->_params['positionId'].'>'.implode('|',$list).')';
				
				
				if($this->_params['condition'] == 'optional') {
					$renderString .= ')?'; 
				} else $renderString .= ')';
				
				

				
			} //end structure dataset
			
			
			
			
			
			
			Zend_Registry::get('dt_log')->log('rendered '.$this->_displayName.':'.$renderString, Zend_Log::DEBUG);			
			return $renderString;		
		}
	}
	

} //end class